/*
 *  Copyright (C) 1993  Robert Davis
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of Version 2, or any later version, of 
 *  the GNU General Public License as published by the Free Software 
 *  Foundation.
 */

static char RCSId[]="$Id: ContourOptionsPanel.m,v 1.9 1993/05/18 03:54:47 davis Exp $";


#import <appkit/Application.h>
#import <appkit/Box.h>
#import <appkit/Button.h>
#import <appkit/Form.h>
#import <appkit/FormCell.h>
#import <appkit/Matrix.h>
#import <appkit/MenuCell.h>
#import <appkit/PopUpList.h>

#import "ContourAutoPane.h"
#import "ContourOptionsPanel.h"
#import "ContourSeriesPane.h"
#import "ContourUserPane.h"
#import "Status.h"
#import "StatusContour.h"


@interface ContourOptionsPanel (Private)
- _setupPane:(Pane *)aPane;
- _selectPane:(int)aPane;
- _swapPane:(Pane *)new;
- _updatePanel;			/** Overridden from OptionsPanel (Private) **/
@end



@implementation ContourOptionsPanel

- init
{
    [super init];

    [NXApp loadNibSection: "ContourOptionsPanel.nib"
		    owner: self
		withNames: NO
		 fromZone: [self zone]];

    [panel setFrameUsingName:"ContourOptionsPanel"];
    [panel setFrameAutosaveName:"ContourOptionsPanel"];

    enabled = NO;

    return self;
}


- free
{
    [seriesPane free];
    [userPane free];
    [autoPane free];
    [[levelsKindButton target] free];

    return [super free];
}


- (BOOL)isEnabled
{
    return enabled;
}


- doSetKind:sender
{
    [status setContourKind:[sender selectedTag]];
    [self _updatePanel];	/* Changing kind may change other controls */
    return self;
}


- doSetPoints:sender
{
    [status setContourPoints:[sender intValue]];

    /* status may not accept points value */
    [pointsField setIntValue:[status contourPoints]];

    return self;
}


- doSetOrder:sender
{
    [status setContourOrder:[sender intValue]];

    /* status may not accept order value */
    [orderField setIntValue:[status contourOrder]];

    return self;
}



- doSetKey:sender
{
    return [status setContourInKey:[sender state]];
}


- doSetLevelsKind:sender
{
    [status setContourLevelsKind:[sender selectedTag]];
    [levelsKindButton setTag:[sender selectedTag]];
    [self _updatePanel];	/* Changing this changes current pane */
    return self;
}



// Shuts up the compiler about unused RCSId
- (const char *) rcsid
{
    return RCSId;
}



@end





@implementation ContourOptionsPanel (Private)

- _setupPane:(Pane *)aPane
{
    if (aPane) {
        View	*panesView = [aPane view];

        [[panel contentView] addSubview: panesView];
        [[panesView allocateGState] lockFocus];
        [panesView unlockFocus];
        [panesView moveTo:0:0];
    }

    return self;
}


- _selectPane:(int)aPane
{
    switch (aPane) {

    case LEVELS_INCREMENTAL:
        if (!seriesPane) {
            seriesPane = [[ContourSeriesPane allocFromZone:[self zone]] init];
            [self _setupPane:seriesPane];
        }
        [self _swapPane:seriesPane];
        break;

    case LEVELS_DISCRETE:
        if (!userPane) {
            userPane = [[ContourUserPane allocFromZone:[self zone]] init];
            [self _setupPane:userPane];
        }
        [self _swapPane:userPane];
        break;

    default:
        if (!autoPane) {
            autoPane = [[ContourAutoPane allocFromZone:[self zone]] init];
            [self _setupPane:autoPane];
        }
        [self _swapPane:autoPane];
        break;

    }

    [currentPane perform:@selector(selectControl:)
		    with:self
	      afterDelay:1
	  cancelPrevious:YES];

    return self;
}



- _swapPane:(Pane *)new
{
    /*
     *  If the new pane is not already visible, move it into the panel.
     */

    if (new != currentPane) {

        [levelsKindBox setContentView: [new view]];
        [currentPane didSwapOut:self];

        [[new didSwapIn:self] updateStatus:status doc:self];
        currentPane = new;

    }

    return self;
}




- _updatePanel
{
    int		contourKind = [status contourKind];
    int		levelsKind = [status contourLevelsKind];

    [panel disableDisplay];

    /* 
     *  If the current status is not nil, update the values of all the 
     *  controls.
     */
    if (status) {

	MenuCell	*cell;

	[kindMatrix selectCellWithTag:contourKind];
	[pointsField setIntValue:[status contourPoints]];
	[orderField setIntValue:[status contourOrder]];
	[keyButton setState:[status contourInKey]];

	cell = [[levelsKindButton target] findCellWithTag:levelsKind];
	[levelsKindButton setTitle:[cell title]];
	[levelsKindButton setTag:levelsKind];

    }

    enabled = status && [status isThreeD] && 
	([status contourBase] ||	// contours don't show on opaque surf.
	 ([status contourSurface] && ![status hiddenThreeD]));

    [kindMatrix setEnabled:enabled];
    [pointsField setEnabled:enabled && (contourKind != CONTOUR_KIND_LINEAR)];
    [orderField setEnabled:enabled && (contourKind == CONTOUR_KIND_BSPLINE)];
    [keyButton setEnabled:enabled];
    [levelsKindButton setEnabled:enabled];
    [levelsKindField setEnabled:enabled];

    /*  setEnabled the title in the box around the matrix, too  */
    [[[[kindMatrix superview] superview] cell] setEnabled:enabled];

    [self _selectPane:levelsKind];
    [currentPane updateStatus:status doc:self];

    [panel reenableDisplay];
    [panel display];

    return self;
}

@end
